package com.cskaoyan.service;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GrouponRules;
import com.cskaoyan.bean.GrouponRulesExample;
import com.cskaoyan.bean.CreateGrouponRulesVO;
import com.cskaoyan.bean.GrouponListRecordVO;
import com.cskaoyan.bean.GrouponListRecordVO_Items;
import com.cskaoyan.bean.GrouponListRecordVO_Items_Assist;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GrouponMapper;
import com.cskaoyan.mapper.GrouponRulesMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GrouponServiceImpl implements GrouponService {

    @Resource
    GrouponRulesMapper grouponRulesMapper;
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    GrouponMapper grouponMapper;

    //查询折扣商品列表
    @Override
    public BaseData queryGrouponRules(Integer page,Integer limit,Integer goodsId,String sort,String order) {

        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(sort + " " +order);

        //设置为：只会显示deleted为false(即未下架)的折扣活动
        grouponRulesExample.createCriteria().andDeletedEqualTo(false);

        PageHelper.startPage(page,limit);
        List<GrouponRules> grouponRulesList = new ArrayList<>();
        if(goodsId == null) {
            grouponRulesList = grouponRulesMapper.selectByExample(grouponRulesExample);
        }
        else {
            grouponRulesList = grouponRulesMapper.selectByGoodsId(goodsId);
        }
        PageInfo<GrouponRules> pageInfo = new PageInfo<>(grouponRulesList);
        Long total = pageInfo.getTotal();
        return new BaseData(grouponRulesList,total);
    }

    //根据商品id(goodsId)判断是否存在这个商品
    @Override
    public Goods queryGoodsByGoodsId(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        return goods;
    }
    //添加折扣商品
    @Override
    public CreateGrouponRulesVO insertGrouponRules(Goods goods, BigDecimal discount, Integer discountMember,
                                       Date expireTime) {
        Integer max = grouponRulesMapper.selectLastInsertId();
        grouponRulesMapper.insertNewGrouponRules(goods.getId(),goods.getName(),goods.getPicUrl(),discount,discountMember,
                new Timestamp(new Date().getTime()),new Timestamp(new Date().getTime()),expireTime,goods.getDeleted());
        CreateGrouponRulesVO createGrouponRulesVO = new CreateGrouponRulesVO(max + 1,goods.getId(),
                goods.getName(),goods.getPicUrl(),discount,discountMember,new Timestamp(new Date().getTime()),
                new Timestamp(new Date().getTime()),expireTime);

        return createGrouponRulesVO;
    }

    /*
    * 更新打折商品的信息
    * 1.先获取修改后的商品的信息
    * 2.将新的商品信息更新到打折表中*/
    @Override
    public void updateGrouponRules(GrouponRules grouponRules) {

        grouponRulesMapper.updateByPrimaryKey(grouponRules);
    }

    /*
    * 删除折扣商品的功能
    * 实际上只是将对应条目的deleted修改为true，然后就不会显示出来了*/
    @Override
    public void deleteGrouponRules(Integer id) {

        grouponRulesMapper.deleteGrouponRules(id);
    }

    /*
    * 获取团购活动列表
    * 1.这个任务太复杂了，需要更高性能的方法，本方法比较低效
    * 2.吃了sql学的不好的亏呐*/
    @Override
    public GrouponListRecordVO getGrouponListRecord(Integer page,Integer limit,Integer goodsId,String sort,String order) {
        Integer offset = (page - 1) * limit;

        /*先解决是否输入goodsId的问题*/
        /*第一步：按创建时间降序排序获取不重复的groupon_id的集合*/
        List<Integer> integers = new ArrayList<>();
        /*如果goodsId不为空，加入查询条件*/
        if(goodsId != null) {
            integers = grouponMapper.getAllGrouponIdByGoodsId(goodsId,sort,order);
        }
        else {
            integers = grouponMapper.getAllGrouponId(sort,order);
        }

        GrouponListRecordVO grouponListRecordVO = new GrouponListRecordVO();
        grouponListRecordVO.setTotal(integers.size());
        /*第二步:遍历integer这个列表，依次查询出每一条GrouponListRecord的数据*/
        if(integers.size() == 0) {
            return grouponListRecordVO;
        }
        List<GrouponListRecordVO_Items> list = new ArrayList<>();
        ;
        /*第三步：将每条数据都封装为GrouponListRecord_Items形式*/
        for(Integer integer : integers) {

            System.out.println(integer);
            GrouponListRecordVO_Items_Assist assist = grouponMapper.getGrouponListRecordAssist(integer);
            GrouponListRecordVO_Items items = new GrouponListRecordVO_Items();
            items.setSubGroupons(grouponMapper.getUserIdAndOrderIdByGrouponId(integer));
            items.setGroupon(assist.getGroupon());
            items.setGoods(assist.getGoods());
            items.setRules(assist.getRules());

            list.add(items);
        }

        /*现在我们已经得到了完整的GrouponListRecord_Items，还需要再创建一个新的GrouponListRecord_Items，
          根据limit和offset放入对应的数据*/
        List<GrouponListRecordVO_Items> itemsList = new ArrayList<>();
        int i = 0;
        for(GrouponListRecordVO_Items items : list) {
            if(i >= offset && i < offset + limit) {
                itemsList.add(items);
            }
            i++;
        }

        grouponListRecordVO.setItems(itemsList);
        return grouponListRecordVO;
    }
}
